const Service = require('../../../core/service/ApiService');

class AgedDao extends Service {

    //小区老人列表
    async getAgedList(pn, ps) {
        const sql = `select aged_id,aged_pic,aged.name as aged_name,town.name as town_name,community.name as community_name,subdistrict.name as subdistrict_name,house_num 
                         from aged 
                         INNER join town on aged.town_id = town.town_id
                         INNER JOIN community on aged.community_id = community.community_id
                         INNER JOIN subdistrict on aged.subdistrict_id = subdistrict.subdistrict_id
                         where status = ${this.constant.AGED_STATUS.ENABLE}
                         limit ${ps}
                         offset ${(pn - 1) * ps}`;
        const result = await this.model.query(sql, {type: this.model.QueryTypes.SELECT});
        return result.length === 0 ? null : result;
    }

    //获取老人信息
    async getAged(id) {
        const sql = `select aged_id,aged_pic,aged.name as aged_name,town.name as town_name,community.name as community_name,subdistrict.name as subdistrict_name,house_num,phone,id_card_num,citizen_card_pic,sex 
                         from aged 
                         INNER join town on aged.town_id = town.town_id
                         INNER JOIN community on aged.community_id = community.community_id
                         INNER JOIN subdistrict on aged.subdistrict_id = subdistrict.subdistrict_id
                         where aged_id = '${id}' and status = ${this.constant.AGED_STATUS.ENABLE}`;
        const result = await this.model.query(sql, {type: this.model.QueryTypes.SELECT});
        return result.length === 0 ? null : result[0];
    }

    //查找城镇和社区
    async findtownandcmy() {
        return this.model.Subdistrict.findAll({
            where: {},
            attributes: ['subdistrict_id', 'town_id', 'community_id'],
            raw: true
        })
    }


    //提交老人信息
    async import(aged_list) {
        return this.model.Aged.bulkCreate(aged_list)
    }
}

module.exports = AgedDao;
